package instantHelper.Raceway;

import java.io.*;
import java.util.HashMap;
import android.app.Activity;
import android.hardware.Camera.*;
import android.os.*;
import android.provider.Settings.Secure;
import android.util.Log;
import android.widget.Button;

public class NormalMode extends Activity {
	Button buttonTakePicture;
	DbHelper myDBHelper;
	private int requestCount = 0;
	int pictureCounter = 0;
	HashMap<String, String> settings = new HashMap<String, String>(5);
	String picturePath = "";
	String androidId = "";
	private double latitude;
	private double longtitude;
	boolean isSendGPS = false;
	private static final String TAG = NormalMode.class.getSimpleName();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.normal_mode);
		buttonTakePicture = (Button) findViewById(R.id.buttonTakePicture);
		buttonTakePicture.setEnabled(false);

		androidId = SettingsUtil.getAndroidID(getApplicationContext());
		try {
			sendingStart();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// previewForNormalMode = new PreviewForNormalMode(this);
		// ((FrameLayout)
		// findViewById(R.id.preview)).addView(previewForNormalMode);

		// buttonTakePicture.setOnClickListener(new OnClickListener() {
		// public void onClick(View v) {
		// previewForNormalMode.camera.takePicture(shutterCallback, rawCallback,
		// jpegCallback);
		// pictureCounter++;
		// }
		// });

		Log.d(TAG, "onCreate'd");
	}

	protected void sendingStart() throws InterruptedException {

		if (settings.get("request_count") != null) {
			requestCount = Integer.parseInt(settings.get("request_count"));

		}
		if (settings.get("sendPicture").equals("1")) {
			enableTakePictureButton();

		}
		// if (settings.get("sendAudio").equals("1")) {
		// try {
		// AudioRecorder audioRecorder = new AudioRecorder(androidId,
		// requestCount);
		// audioRecorder.recordAndSendAudio();
		// } catch (IOException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		// }
		if (settings.get("sendGPS").equals("1")) {
			isSendGPS = true;
		}

		if (settings.get("sendVideo").equals("1")) {
			// enableStartRecordingVideoButton();
		}

		requestCount++;
	}

	private void enableTakePictureButton() {
		buttonTakePicture.setEnabled(true);
	}

	ShutterCallback shutterCallback = new ShutterCallback() {
		public void onShutter() {
			Log.d(TAG, "onShutter'd");
		}
	};

	/** Handles data for raw picture */
	PictureCallback rawCallback = new PictureCallback() {
		public void onPictureTaken(byte[] data, NormalMode camera) {
			Log.d(TAG, "onPictureTaken - raw");
		}

		@Override
		public void onPictureTaken(byte[] data, android.hardware.Camera camera) {
			// TODO Auto-generated method stub

		}
	};

	/** Handles data for jpeg picture */
	PictureCallback jpegCallback = new PictureCallback() {
		public void onPictureTaken(byte[] data, NormalMode camera) {
			FileOutputStream outStream = null;
			try {

				outStream = new FileOutputStream(String.format(
						createPicturePath(), System.currentTimeMillis()));
				outStream.write(data);
				outStream.close();
				Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
			}
			Log.d(TAG, "onPictureTaken - jpeg");
		}

		@Override
		public void onPictureTaken(byte[] data, android.hardware.Camera camera) {
			// TODO Auto-generated method stub

		}
	};

	public String createPicturePath() {
		picturePath = "picture/picture_" + androidId + "_" + requestCount + "_"
				+ pictureCounter + ".jpg";
		picturePath = sanitizePicturePath(picturePath);
		File directory = new File(picturePath).getParentFile();

		try {
			if (!directory.exists() && !directory.mkdirs()) {
				throw new IOException("Path to file could not be created.");
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// onClickTakePicture(buttonClick);

		return picturePath;
	}

	private String sanitizePicturePath(String path) {
		if (!path.startsWith("/")) {
			path = "/" + path;
		}
		if (!path.contains(".")) {
			path += ".jpg";
		}

		return Environment.getExternalStorageDirectory().getAbsolutePath()
				+ path;
	}
}
